package com.legrand.www.module.exception.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiah
 * 2018-06-22 01:24:31
 * description 自定义异常 用来拦截controller及以下异常 无法拦截zuul级别异常  定义异常头部编码为200
 */
@ControllerAdvice
public class CustomizeExceptionHandel {

    @ExceptionHandler(CustomizeException.class)
    @ResponseBody
    /**
     * @author xiah
     * description:拦截所有自定义CustomizeException类异常
     * date: 2018/6/24
     */
    public Map getExceptionMessage(HttpServletResponse httpServletResponse,
                                   CustomizeException e) {
        return getErrorMessage(httpServletResponse, e);
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    /**
     * @author xiah
     * description 自定义异常 用来拦截controller及以下异常 无法拦截zuul级别异常
     * date: 2018/6/24
     */
    public Map getExceptionMessage(HttpServletResponse httpServletResponse, Exception
            e) {
        return getErrorMessage(httpServletResponse, e);
    }

    public Map getErrorMessage(HttpServletResponse httpServletResponse, Exception
            e) {
        Map map = new HashMap(16);
        httpServletResponse.setStatus(200);
        map.put("result", 0);
        map.put("message", e.getMessage());
        return map;
    }
}
